//
//  OrderDetailHeaderView.m
//  JiaoX
//
//  Created by yinqy on 15/1/19.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "OrderDetailHeaderView.h"
#import "LeftLineView.h"

@interface OrderDetailHeaderView()
{
    UILabel *_orderCodeLabel;
    UILabel *_orderStatusLabel;
    UILabel *_orderTimeLabel;
    
    UILabel *_userNameLabel;
    UILabel *_userPhoneLabel;
    UILabel *_userAddressLabel;
    
    LeftLineView *_left3;
    UIView *_splite3;
    
}
@end
@implementation OrderDetailHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        LeftLineView *left1 = [[LeftLineView alloc]initWithTitleFontSize:LEFTLINE_LABEL_FONT_SIZE];
        [left1 setText:@"包裹详情" constrainedToSize:CGSizeMake(JXScreenW-JXPublicInset*2, MAXFLOAT)];
        [left1 setFrame:CGRectMake(JXPublicInset, JXPublicInset, JXScreenW-JXPublicInset*2, left1.label.height)];
        [self addSubview:left1];
        
        JXLog(@"%@",NSStringFromCGRect(left1.frame));
        
        UIView *splite1  = [[UIView alloc]init];
        splite1.frame = CGRectMake(JXPublicInset, CGRectGetMaxY(left1.frame)+JXPublicInset, JXScreenW-JXPublicInset, 0.5);
        [splite1 setBackgroundColor:JXColor(204, 204, 204)];
        [self addSubview:splite1];
        
        //订单号
        _orderCodeLabel = [self getLabelWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(splite1.frame)+JXPublicInset, JXScreenW-JXPublicInset*2, ORDER_DETAIL_FONT_SIZE)];
        //订单状态
        _orderStatusLabel = [self getLabelWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_orderCodeLabel.frame)+JXPublicInset, JXScreenW-JXPublicInset*2, ORDER_DETAIL_FONT_SIZE)];
        //付款时间
        _orderTimeLabel = [self getLabelWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_orderStatusLabel.frame)+JXPublicInset, JXScreenW-JXPublicInset*2, ORDER_DETAIL_FONT_SIZE)];
        
        
        LeftLineView *left2 = [[LeftLineView alloc]initWithTitleFontSize:LEFTLINE_LABEL_FONT_SIZE];
        [left2 setText:@"包裹配送信息" constrainedToSize:CGSizeMake(JXScreenW-JXPublicInset*2, MAXFLOAT)];
        [left2 setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_orderTimeLabel.frame)+30, JXScreenW-JXPublicInset*2, left2.label.height)];
        [self addSubview:left2];
        
        UIView *splite2  = [[UIView alloc]init];
        splite2.frame = CGRectMake(JXPublicInset, CGRectGetMaxY(left2.frame)+JXPublicInset, JXScreenW-JXPublicInset, 0.5);
        [splite2 setBackgroundColor:JXColor(204, 204, 204)];
        [self addSubview:splite2];
        
        
        _userNameLabel = [self getLabelWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(splite2.frame)+JXPublicInset, JXScreenW-JXPublicInset*2, ORDER_DETAIL_FONT_SIZE)];
        _userPhoneLabel = [self getLabelWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_userNameLabel.frame)+JXPublicInset, JXScreenW-JXPublicInset*2, ORDER_DETAIL_FONT_SIZE)];
        _userAddressLabel = [self getLabelWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_userPhoneLabel.frame)+JXPublicInset, JXScreenW-JXPublicInset*2, ORDER_DETAIL_FONT_SIZE)];
        [_userAddressLabel setNumberOfLines:0];
        
        _left3 = [[LeftLineView alloc]initWithTitleFontSize:LEFTLINE_LABEL_FONT_SIZE];
        [_left3 setText:@"商品信息" constrainedToSize:CGSizeMake(JXScreenW-JXPublicInset*2, MAXFLOAT)];
        [_left3 setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_userAddressLabel.frame)+30, JXScreenW-JXPublicInset*2, _left3.label.height)];
        [self addSubview:_left3];
        
        _splite3  = [[UIView alloc]init];
        _splite3.frame = CGRectMake(JXPublicInset, CGRectGetMaxY(_left3.frame)+JXPublicInset, JXScreenW-JXPublicInset, 0.5);
        [_splite3 setBackgroundColor:JXColor(204, 204, 204)];
        [self addSubview:_splite3];
    }
    return self;
}

-(void)setItem:(OrderListItem *)item{
    _item = item;
    [_orderCodeLabel setAttributedText:[self getAttrStr:[NSString stringWithFormat:@"订 单 号：%@",item.orderCode]]];
    [_orderStatusLabel setAttributedText:[self getAttrStr:[NSString stringWithFormat:@"订单状态：%@",item.orderStatusStr]]];
    [_orderTimeLabel setAttributedText:[self getAttrStr:[NSString stringWithFormat:@"付款时间：%@",item.orderCreTime]]];
}

-(void)setAddressDetail:(AddressDetail *)addressDetail{
    _addressDetail = addressDetail;
    [_userNameLabel setAttributedText:[self getAttrStr:[NSString stringWithFormat:@"用 户 名：%@",addressDetail.addressee]]];
    [_userPhoneLabel setAttributedText:[self getAttrStr:[NSString stringWithFormat:@"电话号码：%@",addressDetail.phoneCode]]];
    
    NSString *string = [NSString stringWithFormat:@"地　　址：%@",addressDetail.detailAdres];
    CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:ORDER_DETAIL_FONT_SIZE] constrainedToSize:CGSizeMake(JXScreenW-JXPublicInset*2, MAXFLOAT)];
    [_userAddressLabel setWidth:size.width];
    [_userAddressLabel setHeight:size.height];
    
    [_userAddressLabel setAttributedText:[self getAttrStr:string]];
    
    _left3.y = CGRectGetMaxY(_userAddressLabel.frame)+30;
    _splite3.y = CGRectGetMaxY(_left3.frame)+JXPublicInset;
    
    [self setFrame:CGRectMake(0, 0, JXScreenW, CGRectGetMaxY(_splite3.frame))];
    
}

-(NSAttributedString *)getAttrStr:(NSString *)string{
    
    NSRange range =  [string rangeOfString:@"："];
    NSAttributedString *str = [[NSAttributedString alloc]initWithString:string];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithAttributedString:str];
    
    [text addAttribute:NSForegroundColorAttributeName value: JXColor(153, 153, 153) range:NSMakeRange(0, range.length+range.location)];
    
    [text addAttribute:NSForegroundColorAttributeName value: JXColor(77, 77, 77) range:NSMakeRange(range.length+range.location, string.length-(range.length+range.location))];
    return text;
}


-(UILabel *)getLabelWithFrame:(CGRect)frame{
    UILabel *result =[[UILabel alloc]initWithFrame:frame];
    [result setTextColor:JXColor(153, 153, 153)];
    [result setFont:[UIFont systemFontOfSize:ORDER_DETAIL_FONT_SIZE]];
    [self addSubview:result];
    return result;
}


@end
